package com.maple.phoneward.custom;

import com.maple.phoneward.constants.Constants;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;

/**
 * SmsContentObserver.java
 * 
 * @author: YaJian Qiu
 * @Time 2013-4-7 上午11:26:15<br>
 *       本类描述:短信监听，监听远程短信指令
 */
public class SmsContentObserver extends ContentObserver {

	public static final Uri URI = Uri.parse(Constants.SMS_URI_ALL);
	private Context context;

	public SmsContentObserver(Handler handler, Context context) {
		super(handler);
		this.context = context;
	}

	@Override
	public void onChange(boolean selfChange) {
		// 读取收件箱中的短信，数据库中是以时间降序来存储短信的，所以，第一条记录就是最新的记录
		ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(URI, null, "read=?",new String[]{"0"}, "date desc");
		// 获取第一条短信
		if (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex("_id"));// 短信ID
			String body = cursor.getString(cursor.getColumnIndex("body"));// 短信内容
			String address = cursor.getString(cursor.getColumnIndex("address"));// 发送号码
			String read = cursor.getString(cursor.getColumnIndex("read"));// 发送号码
			System.out.println("id=" + id + "  body=" + body + "  address=" + address  + " read=" + read);
		}
	}

}
